﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Newtonsoft.Json;
using Aishow.Model;
using Aishow.BLL;
using Aishow.IBLL;

namespace Aishow.Website
{
    public partial class PersonalEdit : System.Web.UI.Page
    {
        private IUserManager UserManager = new UserManager();
        private District District;
        private int userId;
        private void BindProvince(string ProvinceName)
        {

            ddlProvince.DataSource = District.Provinces;
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceName";
            ddlProvince.DataBind();   
            ddlProvince.Items.Insert(0,"--请选择--");
            if (string.IsNullOrEmpty(ProvinceName))
            {
                ddlProvince.SelectedIndex = 0;

            }
            else
            {
                ddlProvince.SelectedValue = ProvinceName;
            }
                 
        }
        private void BindCity(string ProvinceName,string CityName) 
        {   var Province = District.Provinces.SingleOrDefault(p => p.ProvinceName == ProvinceName);
            if (Province!=null)
            {
                ddlCity.DataSource = Province.Citys;
                ddlCity.DataTextField = "CityName";
                ddlCity.DataValueField = "CityName";
                ddlCity.DataBind();
            }
           ddlCity.Items.Insert(0, "--请选择--");
            if (string.IsNullOrEmpty(CityName))
            {
                ddlCity.SelectedIndex = 0;

            }
            else
            {
                ddlCity.SelectedValue = CityName;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            var file = Server.MapPath("~/App_Data/district.json");
            var text= File.ReadAllText(file,System.Text.Encoding.Default);
            District=JsonConvert.DeserializeObject<District>(text);

            if (!IsPostBack)
            {
                var ProvinceName = "";
                var CityName = "";
              
                var user = IdentityManager.ReadUser();
                if (user==null)
                {
                    Response.Redirect("login.aspx?ReturnUrl=PersonaEdit.aspx");
                }
                else
                {
                    userId = user.Id;
                    //省市
                    if (!string.IsNullOrWhiteSpace(user.Address))
                    { 
                    string[] addressArray = user.Address.Split(' ');
                    if (addressArray.Length == 2)
                    {
                        ProvinceName = addressArray[0];
                        CityName = addressArray[1];

                    }
                 }
                    //兴趣
                    if (!string.IsNullOrWhiteSpace(user.Hobby))
                    {
                        string[] hobbys= user.Hobby.Split(',');
                        foreach (ListItem item in cblHobby.Items)
                        {
                            var hobby = item.Text;
                           item.Selected= hobbys.Any(p=>p==hobby);

                        }
                    }
                    txtNickName.Text = user.NickName;
                    txtBirthday.Text = user.Birthday?.ToString("yyyy-MM-dd");
                    ddlSex.SelectedValue = user.Sex.ToString();

             }  
                BindProvince(ProvinceName);
                BindCity(ProvinceName, CityName);
            }
        }   

        protected void btnProfile_Click(object sender, EventArgs e)
        {
            var user = IdentityManager.ReadUser();

            user.NickName = txtNickName.Text;
            user.Address = ddlProvince.SelectedValue + " " + ddlCity.SelectedValue;
            user.Birthday = Convert.ToDateTime(txtBirthday.Text);
            user.Sex = Convert.ToInt32(ddlSex.SelectedValue);
               
            
            string hobby = ""; 
            foreach (ListItem item in cblHobby.Items)
            {
                if (item.Selected)
                {
                    hobby += item.Text + ",";
                }
            }
            if (hobby.Length>1)
                user.Hobby = hobby.Substring(0, hobby.Length - 1);
            else
                user.Hobby = hobby;
            
            var result=UserManager.EditWithoutAvatar(user);
            if (result.StatusCode == Model.OperResult.StatusCode.Succeed)
            {
                Response.Redirect("Personal.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "editerror", $"alert('{result.Message}')", true);
            }
            
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            var p = ddlProvince.SelectedValue;
            BindCity(p, "");
        }
    }
}